package cn.hml.basic.service.impl;

import cn.hml.basic.exception.BusinessException;
import cn.hml.basic.service.IVerifyCodeServiece;
import cn.hml.basic.util.StrUtils;
import cn.hml.basic.util.VerifyCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;
import cn.hml.user.mapper.UserMapper;
import cn.hml.user.domain.User;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class VerifyCodeServieceImpl implements IVerifyCodeServiece {

@Autowired
private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;

    @Override
    public String getImageCode(String key) {
        //1.通过StrUilts生成4位的图形验证码code - 字符串
        String code = StrUtils.getComplexRandomString(4);

        //2.将key和图形验证码code保存到redis，5分钟后过期
        redisTemplate.opsForValue().set(key, code, 5, TimeUnit.MINUTES);

        //3.将图形验证码写到图片中并且使用base64编码 - 调用VerifyCodeUtils
        String base64Str = VerifyCodeUtils.verifyCode(115, 40, code);
        return base64Str;
    }
        @Override
        public void getSmsCode(Map<String, String> map) {
            String phone = map.get("phone");
            String imageCode = map.get("imageCode");
            String imageCodeKey = map.get("imageCodeKey");
            //校验空值
            if (StringUtils.isEmpty(phone)||StringUtils.isEmpty(imageCode)){
                throw new BusinessException("信息不能为空");
            }
            //校验图像验证码是否过期,是否正确
            Object obj=redisTemplate.opsForValue().get(imageCodeKey);
            if( obj==null) {
                throw new BusinessException("图片验证码已过期");
            }
            if (!imageCode.equalsIgnoreCase(obj.toString())){
                throw new BusinessException("图形验证码不正确");
            }
            //校验手机是否注册过
            User user=userMapper.loadByPhone(phone);
            if (user!=null){
                throw new BusinessException("改手机号已经注册过,请直接登录,忘记密码请联系管理员");
            }
            //四：直接从redis获取手机验证码：key【业务🗡:手机号】
            Object phoneCode = redisTemplate.opsForValue().get("register:" + phone);

            String code=null;
            //判断是否为空=>过期了或第一次发送
            if(phoneCode==null){
                code = StrUtils.getRandomString(4);
            }else {
                //判断时间是否为违规操作
                Long oldTime=Long.parseLong(phoneCode.toString().split(":")[1]);
                Long newTime=System.currentTimeMillis();
                if ((newTime-oldTime)<1*60*1000){
                    throw new BusinessException("操作频繁，稍后重试!!!");
                }
                code=phoneCode.toString().split(":")[0];
            }
            //五：讲key和验证码保存在redis，重新设置过期时间3分钟【如果是用以前的-这句话就刷新了过期时间】
            redisTemplate.opsForValue().set("register:"+phone,code+":"+System.currentTimeMillis(),3,TimeUnit.MINUTES);
            //六：调用短信的工具类发送短信
            //SmsUtils.sendSms(phone,"你的手机验证码是：" + code +",请在3分钟内使用!!!");
            System.out.println("你的手机验证码是：" + code +",请在3分钟内使用!!!");



    }
}
